import { _decorator, Component, Node, UITransform, Sprite, Animation, SpriteFrame, AnimationClip, animation } from 'cc'
const { ccclass, property } = _decorator

import { TILE_WIDTH, TILE_HEIGHT } from '../Scripts/Tile/TileMapManager'

// 状态机
import { StateMchine } from './StateMchine'

// 状态机类型
import { DIRECTION, PLAYER_STATE_TYPE_ENUM, ENTITY_TYPE_ENUM, SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM } from '../Enums'

// 实体类型
import { ISpikes } from '../Levels'

// utils
import { generateRandomId } from '../Utils'

@ccclass('SpkiesEntityManager')
export class SpkiesEntityManager extends Component {
  id = generateRandomId()

  x: number = 0
  y: number = 0

  // 状态机
  fsm: StateMchine | null = null

  private _count: number = 0
  private _totalCount: number = 0

  type: ENTITY_TYPE_ENUM

  get count() {
    return this._count
  }

  set count(value: number) {
    this._count = value
    // 通过状态去设置参数
    this.fsm?.setPramers(PLAYER_STATE_TYPE_ENUM.SPIKES_CUR_COUNT, value)
  }

  get totalCount() {
    return this._totalCount
  }

  set totalCount(value: number) {
    this._totalCount = value
    // 通过状态去设置参数
    this.fsm?.setPramers(PLAYER_STATE_TYPE_ENUM.SPIKES_TOTAL_COUNT, value)
  }

  init(params: ISpikes) {
    const sprite = this.addComponent(Sprite)
    sprite.sizeMode = Sprite.SizeMode.CUSTOM
    // 设置大小
    const transform = this.getComponent(UITransform)
    transform.setContentSize(TILE_WIDTH * 4, TILE_HEIGHT * 4)
    this.x = params.x
    this.y = params.y

    const type = params.type
    this.type = type
    this.count = params.count
    this.totalCount =
      SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM[type as 'SPIKES_ONE' | 'SPIKES_TWO' | 'SPIKES_THREE' | 'SPIKES_FOUR']
  }

  // 每帧更新
  update() {
    this.node.setPosition(this.x * TILE_WIDTH - 1.5 * TILE_WIDTH, -this.y * TILE_HEIGHT + 1.5 * TILE_HEIGHT)
  }

  onDestroy() {}
}
